package admin

import (
	"example/model"
	"github.com/gin-gonic/gin"
)

func AddHero(c *gin.Context) {
	var h model.HeroModel
	if e := c.ShouldBind(&h); e != nil {
	}
	id := h.Add()
	c.JSON(200, gin.H{"code": 0, "message": "success", "data": id})
}

func UpdateHero(c *gin.Context) {
	var h model.HeroModel
	if e := c.ShouldBind(&h); e != nil {
	}
	res := h.Edit()
	if res {
		c.JSON(200, gin.H{"code": 0, "message": "success"})
	} else {
		c.JSON(200, gin.H{"code": -1, "message": "failed"})
	}
}

func Search(c *gin.Context) {
	var h model.HeroModel
	name := c.Query("name")
	res, e := h.Search(name)
	if e != nil {
		c.JSON(200, gin.H{"code": -1, "message": "failed"})
	} else {
		c.JSON(200, gin.H{"code": 0, "data": res})
	}
}

func DelHero(c *gin.Context) {
	var h model.HeroModel

	id := c.Param("id")

	res := h.Del(id)
	if res {
		c.JSON(200, gin.H{"code": 0, "message": "success"})
	} else {
		c.JSON(200, gin.H{"code": -1, "message": "failed"})
	}
}
